package com.schillerchina.baselibrary.utils;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.widget.Toast;

import com.schillerchina.baselibrary.base.BaseApplication;

public class ToastUtil {

    private static Toast mToast;
    //    Toast点击事件
    public static int duration = 15000;
    public static int ySet = -400;

    private static String infoMsg;

    public static void showToast(final Context context, final String info) {
        if (info == null) {
            return;
        }
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                if (mToast == null||!infoMsg.equals(info)) {
                    mToast = Toast.makeText(BaseApplication.getContext(), info, Toast.LENGTH_LONG);
                } else {
                    mToast.setText(info);
                }
                infoMsg=info;
//                mToast.setGravity(Gravity.CENTER, 0, 0);
                mToast.show();
            }
        });
    }

    public static void showShortToast(String msg) {
        if(BaseApplication.getContext() != null){
            if (mToast == null) {
                mToast = Toast.makeText(BaseApplication.getContext(), msg, Toast.LENGTH_SHORT);
            } else {
                mToast.setText(msg);
            }
            mToast.show();
        }
    }

    public static void showToast(Context context, int stringId) {
        showToast(context, context.getResources().getString(stringId));
    }

    public static void showToast(String msg) {
        showToast(BaseApplication.getContext(), msg);
    }

    public static void showToast(final Throwable e) {
        if (e == null) {
            return;
        }
        showToast(BaseApplication.getContext(), e.getMessage());
    }

    public static void showToast(int msgId) {
        showToast(BaseApplication.getContext(), BaseApplication.getContext().getString(msgId));
    }

    public static void showToastAndKillApp(final String msg) {
        new Thread() {
            @Override
            public void run() {
                Looper.prepare();
                Toast.makeText(BaseApplication.getContext(), msg, Toast.LENGTH_LONG).show();
                Looper.loop();
            }
        }.start();
        try {
            Thread.sleep(1500);
        } catch (Exception e) {
        }
        BaseApplication.killProcess();
    }


}